package isp.apps.example.shoppingcart.persistence.hibernate;

import isp.apps.example.shoppingcart.domain.CreditCard;
import isp.apps.example.shoppingcart.persistence.CreditCardDirectory;
import isp.apps.example.shoppingcart.persistence.PersistenceManagerFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import isp.utils.cjis.persistence.PersistenceException;
import isp.utils.cjis.persistence.PersistenceManager;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class HibernateCreditCardDirectory implements CreditCardDirectory
{

  private PersistenceManager persistenceManager =
    PersistenceManagerFactory.getPersistenceManager();

  private static final Log log =
    LogFactory.getLog(HibernateCreditCardDirectory.class);

  public HibernateCreditCardDirectory()
  {
  }

  /*
  public Item getItemBySku(String sku)
  {

  }

   */


  public List getCreditCards()
  {
    try
    {
      return persistenceManager.find("get.creditCards", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  /**Returns a List of valid CreditCard Objects.  Logs Persistence Exceptions.
   * @return List
   */
  public List getValidCreditCards()
  {
    try
    {
      return persistenceManager.find("get.validCreditCards", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }


  /**Returns a single CreditCard object specified by its ID.
   * @param id
   * @return CreditCard
   */
  public CreditCard getCreditCardById(Integer id)
  {
    try
    {
      List params = new LinkedList();
      params.add(id);
      List l = persistenceManager.find("get.CreditCardById", params);
      if (l.isEmpty())
         return null;
      return (CreditCard)l.get(0);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }
}
